/**
 * Stok3d - getting you stoked on Flash Player native 3D
 * Copyright (C) 2009  David Lenaerts
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author information:
 * David Lenaerts
 * url: http://www.derschmale.com
 * mail: david.lenaerts@gmail.com
 * 
 */

package
{
	import com.derschmale.stok3d.filters.DiffuseFilter;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * Native FP10 3D Diffuse Shading demo
	 */
	
	[SWF(width="600", height="600", frameRate="30", backgroundColor="0x000000")]
	public class DiffuseExample extends Sprite
	{
		[Embed(source="/../exampleAssets/diffuse/brickwork-texture.jpg")]
		private var Texture : Class;
		
		private var _startX : Number;
		private var _startY : Number;
		
		private var _mouseDown : Boolean;
		
		private var _surface : Sprite;
		
		private var _filter : DiffuseFilter;
		
		private var _lightRotation : Number = 0;
		
		private var _glossCount : int = 5;
		
		public function DiffuseExample()
		{
			initView();
			initFilter();
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			addEventListener(Event.ENTER_FRAME, handleEnterFrame);
		}
		
		private function initView() : void
		{
			// init rotatable surface, make sure registration point is in center
			var bmp : Bitmap = new Texture();
			bmp.smoothing = false;
			bmp.x = -bmp.width*.5;
			bmp.y = -bmp.height*.5;
			_surface = new Sprite();
			_surface.addChild(bmp);
			
			_surface.z = 300;
			
			// position and scale
			_surface.x = stage.stageWidth*.5;
			_surface.y = stage.stageHeight*.5;
			
			addChild(_surface);
		}
		
		/**
		 * Create the EnvMapFilter object and assign it to the surface
		 */
		private function initFilter() : void
		{
			_filter = new DiffuseFilter();
			_filter.ambientColor = 0x000020;
			_filter.update(_surface);
			_surface.filters = [ _filter ];
		}
		
		/**
		 * Indicate rotation has started with the current mouse position as motion reference
		 */
		private function handleMouseDown(event : MouseEvent) : void
		{
			_mouseDown = true;
			_startX = mouseX;
			_startY = mouseY;
		}
		
		/**
		 * Indicate rotation has stopped
		 */
		private function handleMouseUp(event : MouseEvent) : void
		{
			_mouseDown = false;
		}
		
		/**
		 * Update filter and surface position
		 */
		private function handleEnterFrame(event : Event) : void
		{
			// move the light's position
			_lightRotation += .03;
			_filter.lightPosition.x = Math.sin(_lightRotation)*500;
			_filter.lightPosition.y = Math.sin(_lightRotation*2)*100;
			_filter.lightPosition.z = Math.cos(_lightRotation)*300-50;
			
			// rotate if mouse is down
			if (_mouseDown) {
		 		_surface.rotationY += (_startX-mouseX)*.01;
		 		_surface.rotationX += (_startY-mouseY)*.01;
		 	}
		 	
		 	// update the filter
		 	_filter.update(_surface);
		 	_surface.filters = [ _filter ];
		}
	}
}